##
# data_schema.py - Created by Timothy Morey on 6/10/2012
#
# This file provides a ctypes wrapper for libnvn items defined in data-schema.h.
#

import ctypes

from libnvn import nvn
import variant


class DimensionSchema(ctypes.Structure):
    _fields_ = [
                ('Name',        ctypes.c_char * 32),
                ('Length',      ctypes.c_size_t),
                ('IsUnlimited', ctypes.c_int),
               ]

    def __str__(self):
        return unicode(self).encode('utf-8')

    def __unicode__(self):
        return self.Name


class DataGridSchema(ctypes.Structure):
    _fields_ = [
                ('Name',      ctypes.c_char * 32),
                ('NDims',     ctypes.c_int),
                ('Dims',      DimensionSchema * 3),
                ('DataType',  ctypes.c_int),
                ('FillValue', variant.Variant),
               ]

    def __str__(self):
        return unicode(self).encode('utf-8')

    def __unicode__(self):
        retval = self.Name
        retval += ' ('
        for i in range(self.NDims):
            retval += unicode(self.Dims[i]) + ', '
        if retval.endswith(', '):
            retval = retval[0:-2]
        retval += ')'
        return retval



class DataSetSchema(ctypes.Structure):
    _fields_ = [
                ('Name',      ctypes.c_char * 32),
                ('NVars',     ctypes.c_int),
                ('Variables', DataGridSchema * 32),
               ]

    def __str__(self):
        return unicode(self).encode('utf-8')

    def __unicode__(self):
        retval = self.Name + '\n'
        for i in range(self.NVars):
            retval += '  ' + unicode(self.Variables[i]) + '\n'
        if retval.endswith('\n'):
            retval = retval[0:-1]
        return retval;
